# -*- coding: utf-8 -*-

from lib.ucloud.ufile import getufilelist
from src.ucloud.ufile_config import read_config


def read_log_list():
    public_key, private_key, bucket = read_config()

    # 构造对象，并设置公私钥
    handler = getufilelist.GetFileList(public_key, private_key)

    # 设置每页的数量
    mylimit = 20

    log_list = []

    # 获取第一页
    ret, resp = handler.getfilelist(bucket, limit=mylimit)

    if ret is None:
        return None
    for item in ret['DataSet']:
        log_list.append(item)

    # 若仍有后续内容, 继续翻页获取
    # while ret['NextMarker'] != "":
    #     ret, resp = handler.getfilelist(bucket, limit=mylimit, marker=ret['NextMarker'])
    #     # assert resp.status_code == 200
    #     # print("count:%d" % len(ret['DataSet']))
    #     for item in ret['DataSet']:
    #         print(item)
    #         log_list.append(item)

    return log_list


def read_log_list_page(page_last_file, page_size, prefix):
    public_key, private_key, bucket = read_config()

    # 构造对象，并设置公私钥
    handler = getufilelist.GetFileList(public_key, private_key)

    # 设置每页的数量
    limit = page_size
    marker = page_last_file
    log_list = []
    # 获取第一页
    # def getfilelist(self, bucket, prefix=None, marker=None, limit=None, header=None):
    ret, resp = handler.getfilelist(bucket, prefix=prefix, marker=marker, limit=limit)

    if ret is None:
        return None, False
    for item in ret['DataSet']:
        log_list.append(item)

    # 若仍有后续内容, 继续翻页获取
    if ret['NextMarker'] != "":
        return log_list, True
    else:
        return log_list, False
